<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		
		<div id="div">
			
		</div>

		<script>
			//创建一个json对象
			var user = {
				"name": "小明",
				"age": 18,
				"hobby": ["打篮球", "打游戏", "听音乐"]
			}
			//使用索引值来访问爱好
			console.log("第一个爱好是: " + user.hobby[0])
			//循环比例所有爱好
			for (var i in user.hobby) {
				console.log(user.hobby[i]);
			}

			//嵌套JSON对象中的数组
			user.sites = [{
					"name": "Google",
					"info": ["Android", "Google 搜索", "Google 翻译"]
				},
				{
					"name": "Runoob",
					"info": ["菜鸟教程", "菜鸟工具", "菜鸟微信"]
				},
				{
					"name": "Taobao",
					"info": ["淘宝", "网购"]
				}
			]
			//遍历数组
			for (var i in user.sites) {
				console.log(user.sites[i].name);
				for(var b in user.sites[i].info){
					console.log(user.sites[i].info[b]);
				}
			}
			
			//修改json
			user.name = "小红"
			//删除数组元素
			delete user.hobby[1]
			
			console.log(user)
			
			//使用JSON.parse转换数据
			var data = '{ "name":"runoob", "alexa":10000, "site":"www.runoob.com" }';
			data = JSON.parse(data);
			console.log(data.name)
			
			//JSON.stringify
			//将JSON转换为字符串
			data = JSON.stringify(data);
			//存入浏览器中
			sessionStorage.setItem("obj",data);
			
			//JavaScript 数组转换
			var arr = [ "Google", "Runoob", "Taobao", "Facebook" ];
			arr = JSON.stringify(arr);
			console.log(arr)
			
			//把 JSON 文本转换为 JavaScript 对象
			var txt = "{name:'asasasa'}";
			var obj = eval("(" + txt + ")");
			document.getElementById("div").innerHTML = obj.name
		</script>
	</body>
</html>